/**
 * copyright © geolabs 2006-2007
 * author: Gérald Fenoy
 *
 */

/**
 * Position Class 
 */
Position=Class.create({
    /**
     * Function used to get the position of the cursor from an event which it
     * has fired.
     *
     * @param e Event Object
     * @return Position ({x: MouseX,y: MouseY}) of the fired event
     */
  fromEvent: function(e){
      var x=0, y=0;
      if (!e) e = System.window.event; // for IE
      if (e.pageX || e.pageY){ 
	// for FF,Moz,Opera7,Safari
	x = e.pageX;
	y = e.pageY;
      }
      else if (e.clientX || e.clientY){ // for IE6,FF,Moz,Opera7
	x = e.clientX + System.document.body.scrollLeft;
	y = e.clientY + System.document.body.scrollTop;
      }
      return new Position(x,y);//{x: x,y: y};
    },

   fromElement: function(obj){
      if( obj.offsetParent ) {
	for(var posX = 0,posY = 0;obj.offsetParent;obj=obj.offsetParent){
	  posX += obj.offsetLeft;
	  posY += obj.offsetTop;
	}
	return { x: posX, y: posY };
      } else {
	return { x: obj.x, y: obj.y };
      }
    },

  /**
  * Function used when you need to have the full size of the navigator
  * window.
  *
  * @return Position {x: with,y: height} the full inner size of the browser 
  * window
  */
  fullSize: function() {
      var width = 0, height = 0;
      if( typeof( System.window.innerWidth ) == 'number' ) {
	//Non-IE
	width = System.window.innerWidth;
	height = System.window.innerHeight;
      } else if( System.document.documentElement && ( System.document.documentElement.clientWidth || System.document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	width = System.document.documentElement.clientWidth;
	height = System.document.documentElement.clientHeight;
      } else if( System.document.body && ( System.document.body.clientWidth || System.document.body.clientHeight ) ) {
	//IE 4 compatible
	width = System.document.body.clientWidth;
	height = System.document.body.clientHeight;
      }
      return new Position(width,height);//{x: width,y: height};//Array(myWidth,myHeight);
    },

  sizeIntoElement: function(elem){
      if( elem.childNodes ) {
	var posX = 0,posY = 0;
	for(var i=0;i<elem.childNodes.length;i++){
	  if(posX<elem.childNodes[i].scrollWidth)
	    posX=elem.childNodes[i].scrollWidth;
	  if(posY<elem.childNodes[i].scrollHeight)
	    posY = elem.childNodes[i].scrollHeight;
	}
	return { x: posX, y: posY };
      } else {
	return { x: elem.scrollWidth, y: elem.scrollWidth };
      }
    },

  getScroll: function(elem){
      var relative_x=((System.document.all)?elem.pageXOffset:elem.scrollLeft);
      var relative_y=((System.document.all)?elem.scrollTop:elem.scrollTop);
      return {x: relative_x, y: relative_y}
    }

  });

Position.define({  
 x:0,
 y:0,
 
 _init: function(){
    if(arguments.length!==2)
      return;
    this.x=arguments[0];
    this.y=arguments[1];
  },

 less: function(pos){
    return new Position(pos.x-this.x,pos.y-this.y);
  }
    
  });


TestPosition=Position.extend();
TestPosition.define({});
